package com.jiezhuo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiezhuo.entity.Role;
import com.jiezhuo.service.RoleService;
import com.jiezhuo.service.UserService;
import com.jiezhuo.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author com.jiezhuo
 * @since 2022-09-19
 */
@RestController
@RequestMapping("/role")
@Api(tags = "角色管理")
public class RoleController {
    @Autowired
    RoleService roleService;
    @ApiOperation("添加修改角色")
    @PostMapping("saveOrUpdateRole")
    public R saveOrUpdateRole(Role role){
        if(StringUtils.isEmpty(role)){
            return R.err().msg("输入角色信息").code(4000);
        }
        boolean b = roleService.saveOrUpdate(role);
        return R.ok().code(2000);
    }

    @ApiOperation("删除角色")
    @PostMapping("deleteRole")
    public R deleteRole(Integer[] ids){
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isEmpty(ids)|| ids.length==0){
            return R.err().msg("输入角色信息").code(4000);
        }
         roleQueryWrapper.in("id",ids);
        int delete = roleService.getBaseMapper().delete(roleQueryWrapper);
        return R.ok().code(2000).msg("删除成功");
    }

    @ApiOperation("查询结构")
    @PostMapping("queryRole")
    public R queryRole(String[] ids,int page,int limit ){
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isEmpty(page) || StringUtils.isEmpty(limit)){
            return R.err().msg("未输入输入page或limit");
        }
        if(!StringUtils.isEmpty(ids) && ids.length>0 ){
            roleQueryWrapper.in("id",ids);
        }

        Page<Role> rolePage = new Page<>(page,limit);
        Page<Role> rolePage1 = roleService.getBaseMapper().selectPage(rolePage, roleQueryWrapper);
        return R.ok().code(2000).data("rolePage",rolePage1);
    }

}

